home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / readline / tilde.c < prev    next >
C/C++ Source or Header  |  1996-11-01  |  10KB  |  385 lines

  1. /* tilde.c -- Tilde expansion code (~/foo := $HOME/foo). */
  2.  
  3. /* Copyright (C) 1988,1989 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Readline, a library for reading lines
  6.    of text with interactive input and history editing.
  7.  
  8.    Readline is free software; you can redistribute it and/or modify it
  9.    under the terms of the GNU General Public License as published by the
  10.    Free Software Foundation; either version 1, or (at your option) any
  11.    later version.
  12.  
  13.    Readline is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with Readline; see the file COPYING.  If not, write to the Free
  20.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #if defined (HAVE_STRING_H)
  27. #  include <string.h>
  28. #else /* !HAVE_STRING_H */
  29. #  include <strings.h>
  30. #endif /* !HAVE_STRING_H */  
  31.  
  32. #if defined (HAVE_STDLIB_H)
  33. #  include <stdlib.h>
  34. #else
  35. #  include "ansi_stdlib.h"
  36. #endif /* HAVE_STDLIB_H */
  37.  
  38. #include "tilde.h"
  39. #include <sys/types.h>
  40. #include <pwd.h>
  41.  
  42. #if defined (USG) && !defined (HAVE_GETPW_DECLS)
  43. extern struct passwd *getpwuid (), *getpwnam ();
  44. #endif /* USG && !defined (HAVE_GETPW_DECLS) */
  45.  
  46. #if !defined (savestring)
  47. extern char *xmalloc ();
  48. #  ifndef strcpy
  49. extern char *strcpy ();
  50. #  endif
  51. #define savestring(x) strcpy (xmalloc (1 + strlen (x)), (x))
  52. #endif /* !savestring */
  53.  
  54. #if !defined (NULL)
  55. #  if defined (__STDC__)
  56. #    define NULL ((void *) 0)
  57. #  else
  58. #    define NULL 0x0
  59. #  endif /* !__STDC__ */
  60. #endif /* !NULL */
  61.  
  62. #if defined (TEST) || defined (STATIC_MALLOC)
  63. static char *xmalloc (), *xrealloc ();
  64. #else
  65. extern char *xmalloc (), *xrealloc ();
  66. #endif /* TEST || STATIC_MALLOC */
  67.  
  68. /* The default value of tilde_additional_prefixes.  This is set to
  69.    whitespace preceding a tilde so that simple programs which do not
  70.    perform any word separation get desired behaviour. */
  71. static char *default_prefixes[] =
  72.   { " ~", "\t~", (char *)NULL };
  73.  
  74. /* The default value of tilde_additional_suffixes.  This is set to
  75.    whitespace or newline so that simple programs which do not
  76.    perform any word separation get desired behaviour. */
  77. static char *default_suffixes[] =
  78.   { " ", "\n", (char *)NULL };
  79.  
  80. /* If non-null, this contains the address of a function to call if the
  81.    standard meaning for expanding a tilde fails.  The function is called
  82.    with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
  83.    which is the expansion, or a NULL pointer if there is no expansion. */
  84. CPFunction *tilde_expansion_failure_hook = (CPFunction *)NULL;
  85.  
  86. /* When non-null, this is a NULL terminated array of strings which
  87.    are duplicates for a tilde prefix.  Bash uses this to expand
  88.    `=~' and `:~'. */
  89. char **tilde_additional_prefixes = default_prefixes;
  90.  
  91. /* When non-null, this is a NULL terminated array of strings which match
  92.    the end of a username, instead of just "/".  Bash sets this to
  93.    `:' and `=~'. */
  94. char **tilde_additional_suffixes = default_suffixes;
  95.  
  96. /* Find the start of a tilde expansion in STRING, and return the index of
  97.    the tilde which starts the expansion.  Place the length of the text
  98.    which identified this tilde starter in LEN, excluding the tilde itself. */
  99. static int
  100. tilde_find_prefix (string, len)
  101.      char *string;
  102.      int *len;
  103. {
  104.   register int i, j, string_len;
  105.   register char **prefixes = tilde_additional_prefixes;
  106.  
  107.   string_len = strlen (string);
  108.   *len = 0;
  109.  
  110.   if (!*string || *string == '~')
  111.     return (0);
  112.  
  113.   if (prefixes)
  114.     {
  115.       for (i = 0; i < string_len; i++)
  116.     {
  117.       for (j = 0; prefixes[j]; j++)
  118.         {
  119.           if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
  120.         {
  121.           *len = strlen (prefixes[j]) - 1;
  122.           return (i + *len);
  123.         }
  124.         }
  125.     }
  126.     }
  127.   return (string_len);
  128. }
  129.  
  130. /* Find the end of a tilde expansion in STRING, and return the index of
  131.    the character which ends the tilde definition.  */
  132. static int
  133. tilde_find_suffix (string)
  134.      char *string;
  135. {
  136.   register int i, j, string_len;
  137.   register char **suffixes = tilde_additional_suffixes;
  138.  
  139.   string_len = strlen (string);
  140.  
  141.   for (i = 0; i < string_len; i++)
  142.     {
  143.       if (string[i] == '/' || !string[i])
  144.     break;
  145.  
  146.       for (j = 0; suffixes && suffixes[j]; j++)
  147.     {
  148.       if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
  149.         return (i);
  150.     }
  151.     }
  152.   return (i);
  153. }
  154.  
  155. /* Return a new string which is the result of tilde expanding STRING. */
  156. char *
  157. tilde_expand (string)
  158.      char *string;
  159. {
  160.   char *result, *tilde_expand_word ();
  161.   int result_size, result_index;
  162.  
  163.   result_size = result_index = 0;
  164.   result = (char *)NULL;
  165.  
  166.   /* Scan through STRING expanding tildes as we come to them. */
  167.   while (1)
  168.     {
  169.       register int start, end;
  170.       char *tilde_word, *expansion;
  171.       int len;
  172.  
  173.       /* Make START point to the tilde which starts the expansion. */
  174.       start = tilde_find_prefix (string, &len);
  175.  
  176.       /* Copy the skipped text into the result. */
  177.       if ((result_index + start + 1) > result_size)
  178.     result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
  179.  
  180.       strncpy (result + result_index, string, start);
  181.       result_index += start;
  182.  
  183.       /* Advance STRING to the starting tilde. */
  184.       string += start;
  185.  
  186.       /* Make END be the index of one after the last character of the
  187.      username. */
  188.       end = tilde_find_suffix (string);
  189.  
  190.       /* If both START and END are zero, we are all done. */
  191.       if (!start && !end)
  192.     break;
  193.  
  194.       /* Expand the entire tilde word, and copy it into RESULT. */
  195.       tilde_word = (char *)xmalloc (1 + end);
  196.       strncpy (tilde_word, string, end);
  197.       tilde_word[end] = '\0';
  198.       string += end;
  199.  
  200.       expansion = tilde_expand_word (tilde_word);
  201.       free (tilde_word);
  202.  
  203.       len = strlen (expansion);
  204.       if ((result_index + len + 1) > result_size)
  205.     result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
  206.  
  207.       strcpy (result + result_index, expansion);
  208.       result_index += len;
  209.       free (expansion);
  210.     }
  211.  
  212.   result[result_index] = '\0';
  213.  
  214.   return (result);
  215. }
  216.  
  217. /* Do the work of tilde expansion on FILENAME.  FILENAME starts with a
  218.    tilde.  If there is no expansion, call tilde_expansion_failure_hook. */
  219. char *
  220. tilde_expand_word (filename)
  221.      char *filename;
  222. {
  223.   char *dirname;
  224.  
  225.   dirname = filename ? savestring (filename) : (char *)NULL;
  226.  
  227.   if (dirname && *dirname == '~')
  228.     {
  229.       char *temp_name;
  230.       if (!dirname[1] || dirname[1] == '/')
  231.     {
  232.       /* Prepend $HOME to the rest of the string. */
  233.       char *temp_home = (char *)getenv ("HOME");
  234.  
  235.       /* If there is no HOME variable, look up the directory in
  236.          the password database. */
  237.       if (!temp_home)
  238.         {
  239.           struct passwd *entry;
  240.  
  241.           entry = getpwuid (getuid ());
  242.           if (entry)
  243.         temp_home = entry->pw_dir;
  244.         }
  245.  
  246.       temp_name = xmalloc (1 + strlen (&dirname[1])
  247.                  + (temp_home ? strlen (temp_home) : 0));
  248.       temp_name[0] = '\0';
  249.       if (temp_home)
  250.         strcpy (temp_name, temp_home);
  251.       strcat (temp_name, dirname + 1);
  252.       free (dirname);
  253.       dirname = temp_name;
  254.     }
  255.       else
  256.     {
  257.       char *username;
  258.       struct passwd *user_entry;
  259.       int i;
  260.  
  261.       username = xmalloc (strlen (dirname));
  262.       for (i = 1; dirname[i] && dirname[i] != '/'; i++)
  263.         username[i - 1] = dirname[i];
  264.       username[i - 1] = '\0';
  265.  
  266.       if ((user_entry = getpwnam (username)) == 0)
  267.         {
  268.           /* If the calling program has a special syntax for
  269.          expanding tildes, and we couldn't find a standard
  270.          expansion, then let them try. */
  271.           if (tilde_expansion_failure_hook)
  272.         {
  273.           char *expansion;
  274.  
  275.           expansion = (*tilde_expansion_failure_hook) (username);
  276.  
  277.           if (expansion)
  278.             {
  279.               temp_name = xmalloc (1 + strlen (expansion)
  280.                           + strlen (&dirname[i]));
  281.               strcpy (temp_name, expansion);
  282.               strcat (temp_name, &dirname[i]);
  283.               free (expansion);
  284.               free (dirname);
  285.               dirname = temp_name;
  286.             }
  287.         }
  288.           /* We shouldn't report errors. */
  289.         }
  290.       else
  291.         {
  292.           temp_name = xmalloc (1 + strlen (user_entry->pw_dir)
  293.                      + strlen (&dirname[i]));
  294.           strcpy (temp_name, user_entry->pw_dir);
  295.           strcat (temp_name, &dirname[i]);
  296.           free (dirname);
  297.           dirname = temp_name;
  298.         }
  299.       endpwent ();
  300.       free (username);
  301.     }
  302.     }
  303.   return (dirname);
  304. }
  305.  
  306.  
  307. #if defined (TEST)
  308. #undef NULL
  309. #include <stdio.h>
  310.  
  311. main (argc, argv)
  312.      int argc;
  313.      char **argv;
  314. {
  315.   char *result, line[512];
  316.   int done = 0;
  317.  
  318.   while (!done)
  319.     {
  320.       printf ("~expand: ");
  321.       fflush (stdout);
  322.  
  323.       if (!gets (line))
  324.     strcpy (line, "done");
  325.  
  326.       if ((strcmp (line, "done") == 0) ||
  327.       (strcmp (line, "quit") == 0) ||
  328.       (strcmp (line, "exit") == 0))
  329.     {
  330.       done = 1;
  331.       break;
  332.     }
  333.  
  334.       result = tilde_expand (line);
  335.       printf ("  --> %s\n", result);
  336.       free (result);
  337.     }
  338.   exit (0);
  339. }
  340.  
  341. static void memory_error_and_abort ();
  342.  
  343. static char *
  344. xmalloc (bytes)
  345.      int bytes;
  346. {
  347.   char *temp = (char *)malloc (bytes);
  348.  
  349.   if (!temp)
  350.     memory_error_and_abort ();
  351.   return (temp);
  352. }
  353.  
  354. static char *
  355. xrealloc (pointer, bytes)
  356.      char *pointer;
  357.      int bytes;
  358. {
  359.   char *temp;
  360.  
  361.   if (!pointer)
  362.     temp = (char *)malloc (bytes);
  363.   else
  364.     temp = (char *)realloc (pointer, bytes);
  365.  
  366.   if (!temp)
  367.     memory_error_and_abort ();
  368.  
  369.   return (temp);
  370. }
  371.  
  372. static void
  373. memory_error_and_abort ()
  374. {
  375.   fprintf (stderr, "readline: Out of virtual memory!\n");
  376.   abort ();
  377. }
  378.  
  379. /*
  380.  * Local variables:
  381.  * compile-command: "gcc -g -DTEST -o tilde tilde.c"
  382.  * end:
  383.  */
  384. #endif /* TEST */
  385.